import axios from "axios";
import Vue from 'vue'
import {zfilespreview} from './api'
import { message } from "ant-design-vue";
const url =  process.env.VUE_APP_API_BASE_URL
//上传文件
export const Uploadfile = (data) => {
    return axios.post(
        url +
        `/fileupdates/zfiles/uploadImages`,
        data,
        {
            headers: {
                "Content-Type": "application/x-www-form-urlencoded",
                "Jmt-Token": localStorage.getItem("token"),
            },
        }
    )
}


//导出项目
export const exportProject = (data) =>{
    return axios.post(
        url +
          `/fileupdates/zfiles/exportProject`,
          data,
          {
            responseType: "blob",
            headers: {
              "Content-Type": "multipart/form-data",
              "Jmt-Token": localStorage.getItem("token"),
            },
          }
       
    )
}


//压缩包文件流
export const   filewjls = (data,contentDisposition) =>{
       const blob = new Blob([data], {
        type: "'application/zip,charset=utf-8'",
      });
      const url = window.URL.createObjectURL(blob);
      const filenameRegex = /filename[^;=\n]*=((['"])(.*?)\2|[^;\n]*)/;
      const matches = filenameRegex.exec(contentDisposition);
      const name = decodeURIComponent(matches[3]);
      const a = document.createElement("a");
      a.href = url;
      a.download = name;
      a.style.display = "none";
      document.body.appendChild(a);
      // 模拟点击a标签来下载文件
      a.click();
      // 释放URL对象
      window.URL.revokeObjectURL(url);
}

//预览文件
export   const   Filespreview = async (data) =>{
  const res = await zfilespreview({ fileName: data.filepath });
      const blob = new Blob([res], { type: "application/pdf" });
      // 创建一个指向Blob的URL
      const url = window.URL.createObjectURL(blob);
      // 在新标签页中打开PDF
      window.open(url)
}


//下载文件
export const downloadfile = async (data,name) => {
   // 将富文本内容转换为Blob对象
   const blob = new Blob([data], { type: "application/pdf" });
   const url = window.URL.createObjectURL(blob);
   // 创建一个a标签，设置href属性为URL对象的URL，设置download属性为要保存的文件名
   const a = document.createElement("a");
   a.href = url;
   a.download = `${name}.pdf`;
   a.style.display = "none";
   document.body.appendChild(a);
   // 模拟点击a标签来下载文件
   a.click();
   // 释放URL对象
   window.URL.revokeObjectURL(url);
}


//批量下载
export const PLdown = (res) =>{
  // 将富文本内容转换为Blob对象
  const blob = new Blob([res], { type: "'application/zip,charset=utf-8'" });
  const url = window.URL.createObjectURL(blob);
  // 创建一个a标签，设置href属性为URL对象的URL，设置download属性为要保存的文件名
  const a = document.createElement("a");
  a.href = url;
  a.download = `files.zip`;
  a.style.display = "none";
  document.body.appendChild(a);
  // 模拟点击a标签来下载文件
  a.click();
  // 释放URL对象
  window.URL.revokeObjectURL(url);
}


//上传文件
export const uploadPDF = (formData) =>{
  return axios.post(
    url +
      `/fileupdates/zfiles/uploadPDF`,
      formData,
      {
        headers: {
          "Content-Type": "multipart/form-data",
          "Jmt-Token": localStorage.getItem("token"),
        },
      }
)
}


//导出市级别得excel
export const exportshitable = (data) =>{
  return axios.post(
      url +
        `/export/sheet/shi`,
        data,
        {
          responseType: "blob",
          headers: {
            "Content-Type": "multipart/form-data",
            "Jmt-Token": localStorage.getItem("token"),
          },
        }
     
  )
}



//到处项目得excel
export const exportproject = (data) =>{
  return axios.post(
      url +
        `/export/sheet/project`,
        data,
        {
          responseType: "blob",
          headers: {
            "Content-Type": "multipart/form-data",
            "Jmt-Token": localStorage.getItem("token"),
          },
        }
     
  )
}
